$('#userName1').html(sessionStorage.getItem('userName'))
function getPower() {
    $.ajax({
        url: 'http://localhost:8080/oa/t-menu/powerByRole1',
        type: "GET",
        dataType: "json",
        headers: {
            token: "" + token
        },
        success: function (data) {
            if (data.code == 200){
                for (var i = 0 ; i < data.data.length ; i++) {
                    $('#'+data.data[i].menuId+'').attr('style','display: block')
                }
            }else if (data.code == 401) {
                window.location.href = 'login.html'
            }else {
                $.message({
                    message:data.msg,
                    type:'error'
                });
            }
        }
    })
}
function audit() {
    $.ajax({
        url: 'http://localhost:8080/oa/t-diary/listDiary1',
        type: "GET",
        dataType: "json",
        headers: {
            token: "" + token
        },
        success: function (data) {
            if (data.code == 200){
                $('#diaryLen').html(data.data.length)
            }else if (data.code == 401) {
                window.location.href = 'login.html'
            }else {
                $.message({
                    message:data.msg,
                    type:'error'
                });
            }
        }
    })
}
$('#logout').unbind('click').on('click',function () {
    $.ajax({
        url: 'http://localhost:8080/oa/t-user/loginOut?id='+sessionStorage.getItem("id"),
        type: "GET",
        dataType: "json",
        success: function (data) {
            window.location.href = 'login.html'
        }
    })
})
function dialogueMessage() {
    $.ajax({
        url: 'http://localhost:8080/oa/t-dialogue/dialogueMessage?userId='+sessionStorage.getItem("id"),
        type: "POST",
        dataType: "json",
        headers: {
            token: "" + token
        },
        success: function (data) {
            if (data.code == 200){
                var data = data.data
                console.log(data)
                if (data.length != 0){
                    $('#messageLen').attr('style','display:block')
                    $('#messageLen').html(data.length)
                }else {
                    $('#messageLen').attr('style','display:none')
                }
                $('#dialogueMessage').empty();
                var html = '';
                html += '<li>\n' +
                    '          <div class="dropdown-title">你有'+data.length+'条新消息</div>\n' +
                    '       </li>';
                for (var i = 0; i < data.length; i++) {
                    html += '<li>\n' +
                        '       <div class="notif-center">\n' +
                        '          <a onclick="seeMessage(\''+data[i].userBId+'\')">\n' +
                        '             <div class="notif-icon notif-primary"> <i class="la la-user-plus"></i> </div>\n' +
                        '                <div class="notif-content">\n' +
                        '                   <span class="block">'+data[i].userAId+'</span>\n' +
                        '                   <span class="time">'+data[i].createTime+'</span>\n' +
                        '                </div>\n' +
                        '          </a nc>\n' +
                        '       </div>\n' +
                        '    </li>'
                }
                html += '<li>\n' +
                    '       <a class="see-all" href="questions.html"> <strong>查看更多...</strong> <i class="la la-angle-right"></i> </a>\n' +
                    '    </li>'
                $('#dialogueMessage').append(html)

            }else if (data.code == 401) {
                window.location.href = 'login.html'
            }else {
                $.message({
                    message:data.msg,
                    type:'error'
                });
            }
        }
    })
}
function seeMessage(userBId) {
    window.location.href = 'questions.html?userBId='+userBId;
}
function parseUrl(){
    var url=location.href;
    var i=url.indexOf('?');
    if(i==-1)return 0;
    var querystr=url.substr(i+1);
    var arr1=querystr.split('&');
    var arr2=new Object();
    for  (i in arr1){
        var ta=arr1[i].split('=');
        arr2[ta[0]]=ta[1];
    }
    return arr2;
}
$('#resetPass').unbind('click').on('click',function () {
    $('#myModal').modal('show')
    $('#setPass').unbind('click').on('click',function () {
        var oldPass = $('#oldPass').val()
        var newPass = $('#newPass').val()
        var newPassTwo = $('#newPassTwo').val()
        if (oldPass.replace(/\s+/g, "") == ""){
            $.message({
                message:'请输入旧密码',
                type:'warning'
            });
            return;
        }
        if (newPass.replace(/\s+/g, "") == ""){
            $.message({
                message:'请输入新密码',
                type:'warning'
            });
            return;
        }
        if (newPassTwo.replace(/\s+/g, "") == ""){
            $.message({
                message:'请输入确认密码',
                type:'warning'
            });
            return;
        }
        if (oldPass != sessionStorage.getItem("userPwd")){
            $.message({
                message:'旧密码不正确',
                type:'warning'
            });
            return;
        }
        if (newPass != newPassTwo){
            $.message({
                message:'新密码和确认密码不一致',
                type:'warning'
            });
            return;
        }
        $.ajax({
            url: 'http://localhost:8080/oa/t-user/setPass',
            data:{userId:sessionStorage.getItem("id"),userPwd:newPass},
            type: "POST",
            dataType: "json",
            headers: {
                token: "" + token
            },
            success: function (data) {
                if (data.code == 200){
                    $.message(data.msg);
                    window.location.href = 'login.html'
                }else if (data.code == 401) {
                    window.location.href = 'login.html'
                }else {
                    $.message({
                        message:data.msg,
                        type:'error'
                    });
                }
            }
        })
    })
})